package _05_stack.my_implements.exerc;

import org.junit.Test;

import java.util.LinkedList;

/**
 * @author: mornd
 * @dateTime: 2023/6/7 - 15:55
 * 使用两个队列模拟栈
 */
public class MyStack1 {
    @Test
    public void test() {
        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.empty());
    }

    static class Stack {
        LinkedList queue = new LinkedList();
        LinkedList queue2 = new LinkedList();

        public void push(int x) {
            queue.offer(x);
            while (!queue2.isEmpty()) {
                queue.offer(queue2.poll());
            }
            LinkedList t = queue;
            queue = queue2;
            queue2 = t;
        }

        public int pop() {
            return (int) queue2.pop();
        }

        public int top() {
            return (int) queue2.peek();
        }

        public boolean empty() {
            return queue2.isEmpty();
        }
    }
}
